<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <!-- 手写call方法 -->
    <script>
      // fn为一个函数与  context为一个对象
      // function call(fn, context = window) {
      //   if (typeof fn !== 'function') {
      //     throw new Error('error');
      //   }
      //   context.fn = fn;
      //   let args = [...arguments].slice(2);
      //   let result = context.fn(...args);
      //   delete context.fn;
      //   return result;
      // }
      // let obj = {
      //   name: 'zs',
      // };
      // function getName() {
      //   return this.name;
      // }
      // console.log(call(getName, obj));

      function deepCopy(obj) {
        let newObj = obj instanceof Array ? [] : {};
        for (let [k, v] of Object.entries(obj)) {
          newObj[k] = typeof v == 'object' ? deepCopy(v) : v;
        }
        return newObj;
      }

      let obj = {
        name: 'zs',
        age: 19,
        data: [0, 1, 2],
      };
      let newObj = deepCopy(obj);
      newObj.data[0] = 4;
      console.log(obj);
      console.log(newObj);
    </script>
  </body>
</html>
